//
//  LableScrollController.m
//  糗百——王桂敏
//
//  Created by 王桂敏 on 15/5/29.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "LableView.h"


@implementation LableView

- (void)_setupWithTitles:(NSArray *)titles{
    for (int i = 0; i < titles.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(CGRectGetWidth([UIScreen mainScreen].bounds) / 5 * i, 0, CGRectGetWidth([UIScreen mainScreen].bounds) / 5, 44);
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        [button setTitle:titles[i] forState:UIControlStateHighlighted];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
        button.tag = 100 + i;
        if (i == 0) {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        [button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
}

- (void)handleButtonAction:(UIButton *)sender{
    if (self.delegate && [self.delegate respondsToSelector:@selector(actionWithButton:)]) {
        [self.delegate actionWithButton:sender];
    }
}

+ (LableView *)defaultLableView:(NSArray *)titles{
    static LableView *lableView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
#warning autorelease
        lableView = [[[self alloc] initWithTitles:titles] autorelease];
    });
    return lableView;
}

- (id)initWithTitles:(NSArray *)titles{
    self = [super init];
    self.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), 44);
    if (self) {
        [self _setupWithTitles:titles];
        self.backgroundColor = [UIColor lightGrayColor];
    }
    return self;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
